# -*- coding: utf-8 -*-
import os
import numpy
import math
import json

rssi_array = numpy.array([])

with open('rssi_array.json', 'r') as f:
    rssi_list = json.loads(f.read())
    rssi_array = numpy.array(rssi_list)

#print rssi_array[3]
def norm(rssi_array,rssi_num):
    begin_index = -1
    end_index = -2
    for i in range(len(rssi_num)):
        if begin_index and rssi_num[i] != 0:
            begin_index = i
        if rssi_num[i] != 0:
            end_index = i
    end_index += 1
    if begin_index != -1 and end_index !=-1:
        return numpy.linalg.norm(rssi_array[begin_index:end_index] - rssi_num[begin_index:end_index])
    elif begin_index !=-1:
        return numpy.linalg.norm(rssi_array[begin_index:] - rssi_num[begin_index:])
    else:
        return -1
    
def get_distance(rssi_num):
    #min_diatance=numpy.linalg.norm(rssi_array[0] - rssi_num)
    min_diatance=norm(rssi_array[0] , rssi_num)
    #print min_diatance
    result = 0
    for i in range(1,len(rssi_array)):
        #diatance=numpy.linalg.norm(rssi_array[i] - rssi_num)
        diatance=norm(rssi_array[i] , rssi_num)
        if diatance<min_diatance:
            min_diatance = diatance
            result = i
        #print diatance
    return result

def test():
    print(get_distance(rssi_array[0]))
    print(get_distance(rssi_array[1]))
    print(get_distance(rssi_array[2]))
#test()
